package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDpartementService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import cn.wolfcode.rbac.util.RequriedPermission;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.JSONFunctions;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDpartementService dpartementService;
    @Autowired
    private IRoleService roleService;
    //角色属性

    //员工列表
    @RequriedPermission(name="员工页面",expression = "employee:list")
    @RequestMapping({"/list"})
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){
        model.addAttribute("result",employeeService.query(qo));
        model.addAttribute("depts",dpartementService.list());
//        PageInfo pageInfo = this.employeeService.query(qo);
//        model.addAttribute("result",pageInfo);
        return "employee/list";
    }
    //员工在修改之前需要做的查询工作
    @RequestMapping({"/input"})
//    @ResponseBody
    public String input(Long id,Model model){
        model.addAttribute("depts",dpartementService.list());
        model.addAttribute("roles",roleService.list());
        System.out.println(id);
        if(id != null){
            model.addAttribute("entity",employeeService.get(id));
        }
        return "employee/input";
    }
    //员工新增/编辑，调用同一个方法
    @RequestMapping({"/saveOrUpdate"})
    @ResponseBody
    public JsonResult saveOrUpdate(Employee entity,Long[]ids){
//        employeeService.saveOrUpdate(entity,ids);
//        return "redirect:/employee/list.do";
        try{
            if(entity.getId() == null){
                this.employeeService.saveOrUpdate(entity,ids);
            }else{
                this.employeeService.saveOrUpdate(entity,ids);
            }
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
    //员工删除
    @RequestMapping({"/delete"})
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            employeeService.delete(id);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }

    //员工批量删除btn_abtchDelete
    @RequestMapping({"/abtchDelete"})
    @ResponseBody
    public JsonResult abtchDelete(Long [] ids){
        try {
            employeeService.abtchDelete(ids);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
    //判断用户名是否存在
    //返回的true验证通过，反之不通过
    //false代表用户已存在，反之代表用户不存在
    //boostrap-valid插件要求，必须有valid这个布尔值来判断是否存在
    //所以要返回一个map，map的key就叫valid
    @RequestMapping({"/checkName"})
    @ResponseBody
    public Map<String,Boolean> checkName(String name){
        Employee employee = this.employeeService.selectByName(name);
        Map<String,Boolean> map = new HashMap<>();
        map.put("valid",employee == null);
        return map;
    }
    //下载（导出）
    @RequestMapping({"/exportxls"})
    public void exportxls(HttpServletResponse response) throws Exception{
      //设置文件头信息
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建一个Excel文件
       // Workbook wb = new HSSFWorkbook();
        Workbook wb = this.employeeService.exportxls();
        //吧Excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }
    //上传（导入）
    //上传的时候要用到的参数是
    @RequestMapping({"/importXls"})
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException{
        employeeService.importXls(file);
        return new JsonResult();
    }
}
